package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.CompanyService;
import com.yixun.qfbao.api.service.SubscriptionApiService;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.Subscription;
import com.yixun.qfbao.model.WxmsgModel;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.dto.SubscriptionDto;
import com.yixun.qfbao.model.dto.WxmsgModelDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.service.SubscriptionService;
import com.yixun.qfbao.service.WxmsgModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SubscriptionApiServiceImpl implements SubscriptionApiService {

    @Autowired
    private SubscriptionService subscriptionService;
    @Autowired
    private WxmsgModelService wxmsgModelService;
    @Autowired
    private CompanyService companyService;

    //获取公众号appi,appsecret
    @Override
    public Subscription getSubscriptionInfo(String companyUuid,String domain,String systemId){
       
        //获取公众号信息
        SubscriptionDto subscriptionDto = new SubscriptionDto();
        subscriptionDto.setCompanyUuid(companyUuid);
        subscriptionDto.setSystemId(systemId);
        subscriptionDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
        List<Subscription> list = subscriptionService.selectListByDto(subscriptionDto);
        if(null == list || list.size() == 0){
            //直接返回公司信息中的appid,appsecret
			CompanyDto companyDto = new CompanyDto();
			companyDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			companyDto.setDomain(domain);
			companyDto.setCompanyUuid(companyUuid);
			Company company =  companyService.selectByDto(companyDto);
            Subscription subscription = new Subscription();
            subscription.setAppId(company.getAppId());
            subscription.setAppSecret(company.getAppSecret());
            subscription.setCompanyId(company.getId());
            subscription.setCompanyUuid(company.getCompanyUuid());
            return subscription;
        }
        return list.get(0);
    }

    //获取公众号模板消息
    @Override
    public Map<Integer, WxmsgModel> getSubscriptionModelMsg(String companyUuid, String domain, String systemId){
        // 触发条件:
        // 当systemId 为 1 时,发送的模板消息为案场助手的 systemId
        // 当systemId 为 4 时,发送的模板消息为分销助手的 systemId
        if(systemId.equals(SystemIdEnum.DEVELOPERS.getCode())){
            systemId = SystemIdEnum.ASSISTANT.getCode();
        }else if(systemId.equals(SystemIdEnum.AGENCY_WEB.getCode())){
            systemId = SystemIdEnum.AGENCY_h5.getCode();
        }
        Subscription subscription = getSubscriptionInfo(companyUuid, domain, systemId);
        WxmsgModelDto wxmsgModelDto = new WxmsgModelDto();
        wxmsgModelDto.setSystemId(systemId);
        if(null != subscription.getId()){
            wxmsgModelDto.setSubscriptionId(subscription.getId());
        }
        wxmsgModelDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
        // 如果是分销人员，则不受是否启用控制
        if(systemId != SystemIdEnum.AGENCY_h5.getCode()
        		&& systemId != SystemIdEnum.WEAPP_AGENCY.getCode()
				&& systemId != SystemIdEnum.WEAPP_AGENCY_WX_ACCOUNTS.getCode()) {
        	wxmsgModelDto.setEnabled(1);
        }
        List<WxmsgModel> list = wxmsgModelService.selectListByDto(wxmsgModelDto);
        if(null == list || list.size() == 0){
            return new HashMap<>();
        }
        return  list.stream().collect(Collectors.toMap(WxmsgModel::getWxModelType, a -> a,(k1, k2)->k1));
    }
}
